package com.zzgc.modules.controller;

import com.zzgc.common.entity.*;
import com.zzgc.common.response.HttpStatus;
import com.zzgc.common.response.Result;
import com.zzgc.modules.param.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * 数据分析
 *
 * @Author:
 * @CreateTime: 2022-12-02  17:27
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping("analysis")
@Api(tags = "数据分析类")
public class DataAnalysisController extends BaseController {

    /**
     * 设备
     * 学校总人数 (人)                      年级总人数               班级总人数
     * 按学年查询总人数
     * 检查覆盖率 (百分比)
     * 视力不良率 (百分比)
     * 近视率 (百分比)
     * 性别分布 男/女(人)
     * 视力人数统计  视力异常/视力正常(人)
     * 年级视力不良人数统计 (人数)            班级视力不良人数统计      视力统计(按度数统计)
     * 年级视力不良趋势统计 (百分比)          班级视力不良趋势统计
     */
    @GetMapping("getCounty")
    @ApiOperation(value = "区县数据分析")
    public Result getCountyAnalysis(String year, Integer number) {
        int userRole = Integer.valueOf(super.getUserRole());
        if (userRole > 4) {
            return Result.builder().code(401).msg("用户无权限！").build();
        }
        String departId = super.getDepartId();
        if (departId == null){
            return Result.builder().code(400).msg("用户不属于任何部门，请联系管理员！").build();
        }
        return getCounty(year, number, Long.valueOf(departId));
    }

    @GetMapping("AutoGetCounty")
    @ApiOperation(value = "自动上传区县数据分析")
    public Result AutoGetCountyAnalysis(String year, Integer number) {
        return getCounty(year, number, null);
    }

    /**
     *
     * @Author
     * @Description
     * @Date  2024/5/15 10:56
     * @Param year 查询的年份
     * @Param number 查询筛查次数
     * @Param departId 用户的部门ID
     * @return
     */
    public Result getCounty(String year, Integer number, Long departId) {
        CountyParam countyParam = new CountyParam();
        StudentArchivesParam archivesParam = new StudentArchivesParam();
        if (year == null || year == "") { // 默认最近一年
            year = String.valueOf(getsYear());
        }
        Student student = new Student();
        student.setSYear(year);
        // 全县总人数
        long count = 0;
        if (number == null) { // 获取默认的1 次数
            number = 1;
        }
        archivesParam.setSYear(year);
        archivesParam.setSNumber(number);
        List<String> list = null;
        // 区县名字
        String departName = "";
        if (departId == null) {
            count = studentService.count(student);
            list = schoolTableDao.queryAllTableName(super.getDatabaseName(), super.getTableName());
            if (list == null || list.size() == 0) {
                countyParam.setCount((int) count); // 区县总人数
                countyParam.setCountyName(super.getDepartName() + "_" + year + "学年_第" + number + "次筛查");
                return Result.builder().code(200).msg("暂无数据").data(countyParam).build();
            }
            // 如果有区县，获取区县父级部门的信息
            if (list.size() != 0) {
                String substring = list.get(0).substring(9, 15);
                AuthDepart authDepart = new AuthDepart();
                authDepart.setARegionId(Long.valueOf(substring));
                authDepart.setALevel(2);
                List<AuthDepart> authDeparts = authDepartService.queryByPage(authDepart);
                if (authDeparts.size() != 0) {
                    departName = authDeparts.get(0).getAName();
                }
            }
        }else {
            count = studentService.countByDepartId(departId, year);
            // 获取用户的部门名字
            AuthDepart authDepart = authDepartService.queryById(departId);
            departName = authDepart.getAName();
            // 不考虑有市级管理的话，直接拼接表名
            list = schoolTableDao.queryAllTableName(super.getDatabaseName(), super.getTableName(authDepart.getARegionId() + ""));
            if (list == null || list.size() == 0) {
                countyParam.setCount((int) count); // 区县总人数
                countyParam.setCountyName(super.getDepartName() + "_" + year + "学年_第" + number + "次筛查");
                return Result.builder().code(200).msg("暂无数据").data(countyParam).build();
            }
        }
        archivesParam.setTableNames(list);
        //List<StudentArchivesParam> studentArchivesParams = studentArchivesService.queryAllSchoolByLimitArchives(archivesParam);
        List<DataAnalysisParam> schools = studentArchivesService.queryAllCountySchool(archivesParam);

        if (schools.size() == 0) {
            countyParam.setCount((int) count); // 区县总人数
            countyParam.setCountyName(departName + "_" + year + "学年_第" + number + "次筛查");
            return Result.builder().code(200).msg("暂无数据").data(countyParam).build();
        }
        // 已测学生数量
        countyParam.setInspect(schools.get(0).getInspect());
        countyParam.setSchoolParams(schools);
        int manNum = 0; // 男生近视人数统计
        int womanNum = 0; // 女生近视人数统计
        int primarySchoolU = 0; // 不良人数
        int middleSchoolU = 0;
        int highSchoolU = 0;
        int primarySchoolM = 0; // 近视人数
        int middleSchoolM = 0;
        int highSchoolM = 0;
        int primarySchool = 0; // 学校类型总人数
        int middleSchool = 0;
        int highSchool = 0;
        for (DataAnalysisParam school : countyParam.getSchoolParams()) { // 每个学校
            int uCount = 0; // 视力不良人数统计
            int mCount = 0; // 近视人数统计
            School school1 = new School();
            school1.setSName(school.getSchoolName());
            List<School> schools1 = schoolService.queryByPage(school1);
            student.setSSchoolId(schools1.get(0).getId());
            school.setCount((int) studentService.count(student));
            school.setInspect(school.getArchivesList().size());
            // 遍历学生
            for (StudentArchives studentArchives : school.getArchivesList()) {
                // 统计男女数量
                if (studentArchives.getSSex().equals("男")) {
                    countyParam.setManCount(countyParam.getManCount() + 1);
                } else {
                    countyParam.setWomanCount(countyParam.getWomanCount() + 1);
                }
                // 统计视力异常, 视力不良人数 - 裸眼视力 < 5.0
                float r = Float.parseFloat(studentArchives.getSRightEyeVisual() == null ? "9" : studentArchives.getSRightEyeVisual());
                float l = Float.parseFloat(studentArchives.getSLeftEyeVisual() == null ? "9" : studentArchives.getSLeftEyeVisual());
                if (r < 5.0 || l < 5.0) {
                    countyParam.setAbnormalVision(countyParam.getAbnormalVision() + 1);
                    school.setAbnormalVision(school.getAbnormalVision() + 1);
                    uCount += 1;
                    float r1 = Float.parseFloat(studentArchives.getSRightEquivalentMirror() == null ? "9" : studentArchives.getSRightEquivalentMirror());
                    float l1 = Float.parseFloat(studentArchives.getSLeftEquivalentMirror() == null ? "9" : studentArchives.getSLeftEquivalentMirror());
                    // 统计近视人数, -- 裸眼视力 < 5.0 并且等效球镜度数 < -0.5
                    if (r1 <= -0.50 || l1 <= -0.50) {
                        countyParam.setMyopia(countyParam.getMyopia() + 1);
                        school.setMyopia(school.getMyopia() + 1);
                        mCount += 1;
                        if (studentArchives.getSSex().equals("男")) { // 统计男女视力不良人数
                            manNum += 1;
                        } else {
                            womanNum += 1;
                        }
                    }
                    if (r1 < -6.00 || l1 < -6.00) { // 三级预警 三级放前边否则三级的也会算到二级里
                        countyParam.setThreeCount(countyParam.getThreeCount() + 1);
                    } else if ((r1 < -0.50 && r1 >= -6.00) || (l1 < -0.50 && l1 >= -6.00)) { // 二级预警
                        countyParam.setTwoCount(countyParam.getTwoCount() + 1);
                    }

                } else {
                    countyParam.setNormalVision(countyParam.getNormalVision() + 1);
                    school.setNormalVision(school.getNormalVision() + 1);
                }
            }
            String type = school.getArchivesList().get(0).getTableName();
            if (type.equals("小学")) {
                primarySchoolU += uCount;
                primarySchoolM += mCount;
                primarySchool += school.getArchivesList().size();
            } else if (type.equals("初中")) {
                middleSchoolU += uCount;
                middleSchoolM += mCount;
                middleSchool += school.getArchivesList().size();
            } else if (type.equals("高中")) {
                highSchoolU += uCount;
                highSchoolM += mCount;
                highSchool += school.getArchivesList().size();
            } else {
                System.out.println("type = " + type + "(不为学校类型!)");
            }
            school.setArchivesList(null);
        }
        countyParam.setCount((int) count); // 区县总人数
        countyParam.setCountyName(departName + "_" + year + "学年_第" + number + "次筛查");
        countyParam.setManMyopiaNum(manNum);
        countyParam.setWomanMyopiaNum(womanNum);
        countyParam.setManMyopiaRate(CountyParam.keepDecimals(1, manNum / (float) countyParam.getManCount())); // 男生近视率
        countyParam.setWomanMyopiaRate(CountyParam.keepDecimals(1, womanNum / (float) countyParam.getWomanCount())); // 女生近视率
        if (primarySchool == 0) {
            countyParam.setPrimarySchoolU(0.0f);
            countyParam.setPrimarySchoolM(0.0f);
        } else {
            countyParam.setPrimarySchoolU(CountyParam.keepDecimals(1, primarySchoolU / (float) primarySchool));
            countyParam.setPrimarySchoolM(CountyParam.keepDecimals(1, primarySchoolM / (float) primarySchool));
        }
        if (middleSchool == 0) {
            countyParam.setMiddleSchoolU(0.0f);
            countyParam.setMiddleSchoolM(0.0f);
        } else {
            countyParam.setMiddleSchoolU(CountyParam.keepDecimals(1, middleSchoolU / (float) middleSchool));
            countyParam.setMiddleSchoolM(CountyParam.keepDecimals(1, middleSchoolM / (float) middleSchool));
        }
        if (highSchool == 0) {
            countyParam.setHighSchoolU(0.0f);
            countyParam.setHighSchoolM(0.0f);
        } else {
            countyParam.setHighSchoolU(CountyParam.keepDecimals(1, highSchoolU / (float) highSchool));
            countyParam.setHighSchoolM(CountyParam.keepDecimals(1, highSchoolM / (float) highSchool));
        }
        System.out.println("countyParam = " + countyParam);
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(countyParam)
                .build();
    }


    @GetMapping("getSchool")
    @ApiOperation(value = "学校数据分析")
    public Result getSchoolAnalysis(String year, Integer number, Long schoolId) {
        DataAnalysisParam dataAnalysisParam = new DataAnalysisParam();
        if (year == null || year == "") {
            year = String.valueOf(getsYear());
        }
        String tableName = "archives_";
        Student student = new Student();
        // 判断是否是区县管理员
        if (super.getUserRole().equals("4")) {
            // 判断是否指定学校，不指定就默认
            if (schoolId != null) {
                student.setSSchoolId(schoolId);
                tableName += super.getUserRegionId() + "_" + schoolId;
            } else {
                // 如果没有传学校id ,就默认【本区域】的第一个学校
                School school = new School();
                school.setSDepartId(Long.valueOf(super.getDepartId()));
                List<School> schools = schoolService.queryByPage(school);
                if (schools.size() == 0) {
                    dataAnalysisParam.setCount(0); // 学校总人数
                    return Result.builder().code(200).msg("暂无学校信息").data(dataAnalysisParam).build();
                }
                schoolId = schools.get(0).getId();
                student.setSSchoolId(schoolId);
                tableName += super.getUserRegionId() + "_" + schoolId;
            }
        } else {
            student.setSSchoolId(Long.valueOf(super.getSchoolId()));
            schoolId = Long.valueOf(super.getSchoolId());
            tableName += super.getUserRegionId() + "_" + schoolId;
        }
        student.setSYear(year);
        // 查询学校总人数
        long schoolCount = studentService.count(student);

        // 查询检测数据
        StudentArchives archives = new StudentArchives();
        archives.setTableName(tableName);
        archives.setSSchoolId(schoolId);
        archives.setSYear(year);
        if (number == null) { // 获取默认的最大检测次数
            number = studentArchivesService.maxNumber(archives.getTableName(), archives.getSYear());
        }
        archives.setSNumber(number);
        School school = studentArchivesService.queryAll(archives);
        // 已经测试的学生人数
        long archivesCount = studentArchivesService.count(archives);
        //System.out.println("school = " + school);
        if (school == null) {
            dataAnalysisParam.setCount((int) schoolCount); // 学校总人数
            School school1 = schoolService.queryById(student.getSSchoolId()); // 获取学校名字
            dataAnalysisParam.setSchoolName(school1.getSName() + "_" + year + "学年_第" + number + "次筛查");
            return Result.builder().code(200).msg("暂无数据").data(dataAnalysisParam).build();
        }
        List<GradeParam> gradeList = new ArrayList<>();
        // 年级列表
        for (SchoolGrade schoolGrade : school.getSchoolGradeList()) {
            GradeParam gradeParam = new GradeParam();
            gradeParam.setGradeName(schoolGrade.getSName());
            // 班级列表
            //List<ClassParam> classList = new ArrayList<>();
            for (SchoolGradeClass schoolGradeClass : schoolGrade.getGradeClassList()) {
                //ClassParam classParam = new ClassParam();
                //classParam.setClassName(schoolGradeClass.getSName());
                gradeParam.setInspect(gradeParam.getInspect() + schoolGradeClass.getArchivesList().size());
                // 遍历学生
                for (StudentArchives studentArchives : schoolGradeClass.getArchivesList()) {
                    // 统计男女数量
                    if (studentArchives.getSSex().equals("男")) {
                        dataAnalysisParam.setManCount(dataAnalysisParam.getManCount() + 1);
                    } else {
                        dataAnalysisParam.setWomanCount(dataAnalysisParam.getWomanCount() + 1);
                    }
                    // 统计视力异常, 视力不良人数 - 裸眼视力 < 5.0
                    float r = Float.parseFloat(studentArchives.getSRightEyeVisual() == null ? "9" : studentArchives.getSRightEyeVisual());
                    float l = Float.parseFloat(studentArchives.getSLeftEyeVisual() == null ? "9" : studentArchives.getSLeftEyeVisual());
                    if (r < 5.0 || l < 5.0) {
                        dataAnalysisParam.setAbnormalVision(dataAnalysisParam.getAbnormalVision() + 1);
                        gradeParam.setAbnormalVision(gradeParam.getAbnormalVision() + 1);
                        float r1 = Float.parseFloat(studentArchives.getSRightEquivalentMirror() == null ? "9" : studentArchives.getSRightEquivalentMirror());
                        float l1 = Float.parseFloat(studentArchives.getSLeftEquivalentMirror() == null ? "9" : studentArchives.getSLeftEquivalentMirror());
                        // 统计近视人数, -- 裸眼视力 < 5.0 并且等效球镜度数 < -0.5
                        if (r1 < -0.50 || l1 < -0.50) {
                            dataAnalysisParam.setMyopia(dataAnalysisParam.getMyopia() + 1);
                        }
                        if (r1 < -6.00 || l1 < -6.00) { // 三级预警 三级放前边否则三级的也会算到二级里
                            dataAnalysisParam.setThreeCount(dataAnalysisParam.getThreeCount() + 1);
                        } else if ((r1 < -0.50 && r1 >= -6.00) || (l1 < -0.50 && l1 >= -6.00)) { // 二级预警
                            dataAnalysisParam.setTwoCount(dataAnalysisParam.getTwoCount() + 1);
                        }

                    } else {
                        dataAnalysisParam.setNormalVision(dataAnalysisParam.getNormalVision() + 1);
                        gradeParam.setNormalVision(gradeParam.getNormalVision() + 1);
                    }

                }
//                classList.add(classParam);
//                gradeParam.setClassParams(classList);
            }

            gradeList.add(gradeParam);
        }
        dataAnalysisParam.setGradeParams(gradeList); // 添加年级信息
        dataAnalysisParam.setCount((int) schoolCount); // 学校总人数
        dataAnalysisParam.setInspect((int) archivesCount); // 检查总人数 --- 计算检查覆盖率
        dataAnalysisParam.setSchoolName(school.getSName() + "_" + year + "学年_第" + number + "次筛查"); // 学校名字
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(dataAnalysisParam)
                .build();
    }

    @GetMapping("getGrade")
    @ApiOperation(value = "年级数据分析")
    public Result getGradeAnalysis(String year, Integer number, Long gradeId) {
        GradeParam gradeParam = new GradeParam();
        // 查询年级总人数
        Student student = new Student();
        if (year == null || year == "") {
            year = String.valueOf(getsYear());
        }
        SchoolGrade schoolGrade = new SchoolGrade();
        if (gradeId == null) {
            schoolGrade.setSSchoolId(Long.valueOf(super.getSchoolId()));
            schoolGrade.setSYear(year);
            // 查询当前年份的年级列表
            schoolGrade.setSorts(Arrays.asList(new Sort("s_sort")));
            List<SchoolGrade> schoolGrades = schoolGradeService.queryByPage(schoolGrade);
            if (schoolGrades.size() > 0) {
                schoolGrade = schoolGrades.get(0);
            }
        } else {
            schoolGrade = schoolGradeService.queryById(gradeId);
        }
        // 查询年级
        student.setSSchoolId(Long.valueOf(super.getSchoolId()));
        student.setSYear(year);
        student.setSGradeId(schoolGrade.getId());
        long gradeCount = studentService.count(student);

        // 查询检测数据
        StudentArchives archives = new StudentArchives();
        archives.setTableName(super.getStudentTable());
        archives.setSSchoolId(Long.valueOf(super.getSchoolId()));
        archives.setSYear(year);
        if (number == null) {
            number = studentArchivesService.maxNumber(archives.getTableName(), archives.getSYear());
        }
        archives.setSNumber(number);
        archives.setSGradeName(schoolGrade.getSName());
        School school = studentArchivesService.queryAll(archives);
        //System.out.println("school = " + school);
        if (school == null) {
            gradeParam.setCount((int) gradeCount); // 年级总人数
            gradeParam.setGradeName(super.getDepartName() + "_" + schoolGrade.getSName());
            return Result.builder().code(200).msg("暂无数据").data(gradeParam).build();
        }
        for (SchoolGrade grade : school.getSchoolGradeList()) { // 年级
            List<ClassParam> classList = new ArrayList<>();
            for (SchoolGradeClass schoolGradeClass : grade.getGradeClassList()) { // 班级
                ClassParam classParam = new ClassParam();
                classParam.setClassName(schoolGradeClass.getSName()); // 班级名字
                classParam.setInspect(schoolGradeClass.getArchivesList().size()); // 班级检测人数
                for (StudentArchives studentArchives : schoolGradeClass.getArchivesList()) { // 学生
                    // 统计男女数量
                    if (studentArchives.getSSex().equals("男")) {
                        gradeParam.setManCount(gradeParam.getManCount() + 1);
                    } else {
                        gradeParam.setWomanCount(gradeParam.getWomanCount() + 1);
                    }
                    // 统计视力异常, 视力不良人数 - 裸眼视力 < 5.0
                    float r = Float.parseFloat(studentArchives.getSRightEyeVisual() == null ? "9" : studentArchives.getSRightEyeVisual());
                    float l = Float.parseFloat(studentArchives.getSLeftEyeVisual() == null ? "9" : studentArchives.getSLeftEyeVisual());
                    if (r < 5.0 || l < 5.0) {
                        gradeParam.setAbnormalVision(gradeParam.getAbnormalVision() + 1);
                        classParam.setAbnormalVision(classParam.getAbnormalVision() + 1);
                        if (r < 5.0) {
                            classParam.setAbnormalVisionR(classParam.getAbnormalVisionR() + 1);
                        } else {
                            classParam.setAbnormalVisionL(classParam.getAbnormalVisionL() + 1);
                        }
                        float r1 = Float.parseFloat(studentArchives.getSRightEquivalentMirror() == null ? "9" : studentArchives.getSRightEquivalentMirror());
                        float l1 = Float.parseFloat(studentArchives.getSLeftEquivalentMirror() == null ? "9" : studentArchives.getSRightEquivalentMirror());
                        // 统计近视人数, -- 裸眼视力 < 5.0 并且等效球镜度数 < -0.5
                        if (r1 < -0.50 || l1 < -0.50) {
                            gradeParam.setMyopia(gradeParam.getMyopia() + 1);
                        }
                        if (r1 < -6.00 || l1 < -6.00) { // 三级预警 三级放前边否则三级的也会算到二级里
                            gradeParam.setThreeCount(gradeParam.getThreeCount() + 1);
                        } else if ((r1 < -0.50 && r1 >= -6.00) || (l1 < -0.50 && l1 >= -6.00)) { // 二级预警
                            gradeParam.setTwoCount(gradeParam.getTwoCount() + 1);
                        }
                    } else {
                        gradeParam.setNormalVision(gradeParam.getNormalVision() + 1);
                        classParam.setNormalVision(classParam.getNormalVision() + 1);
                    }
                }
                classList.add(classParam);
            }
            gradeParam.setClassParams(classList);
        }

        // 已经测试的学生人数
        long archivesCount = studentArchivesService.count(archives);
        gradeParam.setCount((int) gradeCount); // 年级总人数
        gradeParam.setInspect((int) archivesCount); // 年级已测总人数
        gradeParam.setGradeName(school.getSName() + "_" + schoolGrade.getSName()); //年级名字
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(gradeParam)
                .build();
    }

    @GetMapping("getClass")
    @ApiOperation(value = "班级数据分析")
    public Result getClassAnalysis(String year, Integer number, Long gradeId, Long classId) {
        ClassParam classParam = new ClassParam();
        // 查询班级总人数
        Student student = new Student();
        if (year == null || year == "") {
            year = String.valueOf(getsYear());
        }
        SchoolGrade grade = new SchoolGrade();
        if (gradeId == null) {
            grade.setSSchoolId(Long.valueOf(super.getSchoolId()));
            grade.setSYear(year);
            // 查询当前年份的年级列表
            grade.setSorts(Arrays.asList(new Sort("s_sort")));
            List<SchoolGrade> schoolGrades = schoolGradeService.queryByPage(grade);
            if (schoolGrades.size() > 0) {
                grade = schoolGrades.get(0);
            }
        } else {
            grade = schoolGradeService.queryById(gradeId);
        }
        SchoolGradeClass schoolGradeClass = new SchoolGradeClass();
        if (classId == null) {
            schoolGradeClass.setSSchoolId(Long.valueOf(super.getSchoolId()));
            schoolGradeClass.setSGradeId(grade.getId());
            List<SchoolGradeClass> schoolGradeClasses = schoolGradeClassService.queryByPage(schoolGradeClass);
            if (schoolGradeClasses.size() > 0) {
                schoolGradeClass = schoolGradeClasses.get(0);
            }
        } else {
            schoolGradeClass = schoolGradeClassService.queryById(classId);
        }
        student.setSSchoolId(Long.valueOf(super.getSchoolId()));
        student.setSYear(year);
        student.setSGradeId(grade.getId());
        student.setSClassId(schoolGradeClass.getId());
        long classCount = studentService.count(student);

        // 查询检测数据
        StudentArchives archives = new StudentArchives();
        archives.setTableName(super.getStudentTable());
        archives.setSSchoolId(Long.valueOf(super.getSchoolId()));
        archives.setSYear(year);
        archives.setSNumber(number);
        archives.setSClassName(schoolGradeClass.getSName());
        archives.setSGradeName(grade.getSName());
        School school = studentArchivesService.queryAll(archives);
        //System.out.println("school = " + school);
        if (school == null) {
            classParam.setCount((int) classCount);
            classParam.setClassName(super.getDepartName() + "_" + grade.getSName() + "_" + schoolGradeClass.getSName());
            return Result.builder().code(200).msg("暂无数据").data(classParam).build();
        }
        HashMap<Float, Integer> hashMapR = new HashMap<>();
        HashMap<Float, Integer> hashMapL = new HashMap<>();
        for (SchoolGrade schoolGrade : school.getSchoolGradeList()) { // 年级
            for (SchoolGradeClass gradeClass : schoolGrade.getGradeClassList()) { // 班级
                classParam.setInspect(gradeClass.getArchivesList().size()); // 检查总人数
                for (StudentArchives studentArchives : gradeClass.getArchivesList()) { // 学生
                    // 统计男女数量
                    if (studentArchives.getSSex().equals("男")) {
                        classParam.setManCount(classParam.getManCount() + 1);
                    } else {
                        classParam.setWomanCount(classParam.getWomanCount() + 1);
                    }
                    // 统计视力异常, 视力不良人数 - 裸眼视力 < 5.0
                    float r = Float.parseFloat(studentArchives.getSRightEyeVisual() == null ? "9" : studentArchives.getSRightEyeVisual());
                    float l = Float.parseFloat(studentArchives.getSLeftEyeVisual() == null ? "9" : studentArchives.getSLeftEyeVisual());

                    hashMapR.put(r, hashMapR.get(r) == null ? 1 : hashMapR.get(r) + 1);
                    hashMapL.put(l, hashMapL.get(l) == null ? 1 : hashMapL.get(l) + 1);

                    if (r < 5.0 || l < 5.0) {
                        classParam.setAbnormalVision(classParam.getAbnormalVision() + 1);
                        float r1 = Float.parseFloat(studentArchives.getSRightEquivalentMirror() == null ? "9" : studentArchives.getSRightEquivalentMirror());
                        float l1 = Float.parseFloat(studentArchives.getSLeftEquivalentMirror() == null ? "9" : studentArchives.getSLeftEquivalentMirror());
                        // 统计近视人数, -- 裸眼视力 < 5.0 并且等效球镜度数 < -0.5
                        if (r1 < -0.50 || l1 < -0.50) {
                            classParam.setMyopia(classParam.getMyopia() + 1);
                        }
                        if (r1 < -6.00 || l1 < -6.00) { // 三级预警 三级放前边否则三级的也会算到二级里
                            classParam.setThreeCount(classParam.getThreeCount() + 1);
                        } else if ((r1 < -0.50 && r1 >= -6.00) || (l1 < -0.50 && l1 >= -6.00)) { // 二级预警
                            classParam.setTwoCount(classParam.getTwoCount() + 1);
                        }
                    } else {
                        classParam.setNormalVision(classParam.getNormalVision() + 1);
                    }
                }
            }
        }
        classParam.setVisionsR(hashMapR);
        classParam.setVisionsL(hashMapL);

        classParam.setCount((int) classCount); // 班级总人数
        classParam.setClassName(school.getSName() + "_" + school.getSchoolGradeList().get(0).getSName() + "_" + schoolGradeClass.getSName()); // 班级名

        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(classParam)
                .build();
    }

    @Autowired
    DropdownController dropdownController;

    @GetMapping("getAllGrade")
    @ApiOperation(value = "当前年份的所有年级数据分析")
    public Result getAllGradeAnalysis(String year, Integer number) {
        SchoolGrade schoolGrade = new SchoolGrade();
        ArrayList<Object> list = new ArrayList<>();
        schoolGrade.setSSchoolId(Long.valueOf(super.getSchoolId()));
        schoolGrade.setSYear(year);
        // 查询当前年份的年级列表
        // 排序
        schoolGrade.setSorts(Arrays.asList(new Sort("s_sort")));
        schoolGrade.setSize(Integer.MAX_VALUE);
        List<SchoolGrade> schoolGrades = schoolGradeService.queryByPage(schoolGrade);
        for (SchoolGrade grade : schoolGrades) {
            // System.out.println("grade = " + grade);
            Result result = getGradeAnalysis(year, number, grade.getId());
            if (result.getCode().equals(200)) {
                list.add(result.getData());
            }
        }
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(list)
                .build();
    }

    @GetMapping("getAllClass")
    @ApiOperation(value = "当前年级的所有班级数据分析")
    public Result getAllClassAnalysis(String year, Integer number, Long gradeId) {
        SchoolGradeClass schoolGradeClass = new SchoolGradeClass();
        schoolGradeClass.setSSchoolId(Long.valueOf(super.getSchoolId()));
        //System.out.println("gradeId = " + gradeId);
        schoolGradeClass.setSGradeId(gradeId);

        ArrayList<Object> list = new ArrayList<>();
        // 查询gradeId有多少个班级
        // 排序
        schoolGradeClass.setSorts(Arrays.asList(new Sort("s_sort")));
        schoolGradeClass.setSize(Integer.MAX_VALUE);
        List<SchoolGradeClass> schoolGradeClasses = schoolGradeClassService.queryByPage(schoolGradeClass);
        for (SchoolGradeClass gradeClass : schoolGradeClasses) {
            //System.out.println("gradeClass = " + gradeClass);
            Result result = getClassAnalysis(year, number, gradeId, gradeClass.getId());
            if (result.getCode().equals(200)) {
                list.add(result.getData());
            }
        }
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(list)
                .build();
    }

}
